package com.cm.utils;

import com.cm.dto.CreateTemplateDto;
import com.cm.entity.lic.element.FontElementEntity;
import com.cm.entity.lic.element.ImgElementEntity;
import com.cm.entity.lic.element.ParamsElementEntity;
import com.cm.entity.lic.page.PagesEntity;
import org.ofdrw.font.Font;
import org.ofdrw.layout.OFDDoc;
import org.ofdrw.layout.VirtualPage;
import org.ofdrw.layout.element.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.file.Paths;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * 核心样式库
 * PageLayout  页码
 * VirtualPage
 * Div 元素
 */
@Component
public class TempStyleUtil {

    private final static Logger logger = LoggerFactory.getLogger(TempStyleUtil.class);

    /**
     * 样式载入
     *
     * @param ofdDoc       ofd文件对象
     * @param templateInfo ofd预处理内容
     * @return 是否成功
     */
    public boolean styleLoad(OFDDoc ofdDoc, CreateTemplateDto templateInfo) {
        AtomicBoolean status = new AtomicBoolean(true);
        // 页面实体
        for (PagesEntity pagesEntity : templateInfo.getPagesInfo()) {// 当前的页面对象
            VirtualPage currentPage = new VirtualPage(ofdDoc.getPageLayout());
            templateInfo.getElementInfo().stream().filter(
                    element ->
                            element.getPageId().equals(pagesEntity.getPageId())).forEach(
                    element -> {
                        // 文字元素
                        if (element instanceof FontElementEntity) {
                            FontElementEntity fontElement = (FontElementEntity) element;
                            // 同步文字元素对象属性
                            Paragraph paragraph = new Paragraph(fontElement.getFontBody());
                            paragraph
                                    .setFontSize(fontElement.getFontSize())
                                    .setDefaultFont(new Font(fontElement.getFont(),fontElement.getFont()));
                            // 向页面对象添加元素对象
                            currentPage.add(paragraph);
                        } else if (element instanceof ImgElementEntity) {
                            ImgElementEntity fontElement = (ImgElementEntity) element;
                            Img img = null;
                            try {
                                img = new Img(Paths.get(fontElement.getUrl()));
                            } catch (IOException e) {
                                logger.warn(e.getMessage());
                                logger.warn("模板创建失败--图片元素导入失败");
                                status.set(false);
                            }
                            img
                                    .setWidth(fontElement.getWidth())
                                    .setHeight(fontElement.getHeight())
                                    .setMarginTop(fontElement.getMarginY())
                                    .setMarginLeft(fontElement.getMarginX())
                                    .setPaddingTop(fontElement.getBorderY())
                                    .setPaddingLeft(fontElement.getPaddingX())
                                    .setBorderTop(fontElement.getBorderY())
                                    .setBorderLeft(fontElement.getBorderX());
                            currentPage.add(img);
                        } else if (element instanceof ParamsElementEntity) {
                            ParamsElementEntity fontElement = (ParamsElementEntity) element;
                            // 同步文字元素对象属性
                            Paragraph paragraph = new Paragraph(fontElement.getParamsFontBody());
                            paragraph
                                    .setFontSize(fontElement.getParamsFontSize())
                                    .setDefaultFont(new Font(fontElement.getParamsFont(),fontElement.getParamsFont()));
                            // 向页面对象添加元素对象
                            currentPage.add(paragraph);
                        }
                    });
            // 将页面对象加入ofd文件对象中
            ofdDoc.addVPage(currentPage);
        }
        return status.get();
    }
}
